package myschool.service;

import myschool.dao.StudentDao;
import myschool.dao.SubjectDao;
import myschool.entity.Page;
import myschool.entity.Student;
import myschool.entity.Subject;
import myschool.util.MybatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class StudentService {
    public static final StudentService ME = new StudentService();

    public static final Logger Log = LoggerFactory.getLogger(StudentService.class);


    public Page<Student> page(int pageNo, int pageSize, String sname, String tele, String QQ,
                              Date enterdateStart, Date enterDateJieshu) {
        SqlSession session = MybatisUtil.get();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            List<Student> list = dao.search(sname, tele, QQ, enterdateStart, enterDateJieshu, pageNo, pageSize);
            int total = dao.search1Total(sname, tele, QQ, enterdateStart, enterDateJieshu);
            return new Page<>(list, pageNo, total, pageSize);
        } finally {
            MybatisUtil.close(session);
        }
    }

    public int add(Student student) {
        SqlSession session = MybatisUtil.get();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            return dao.add(student);
        } finally {
            MybatisUtil.close(session);
        }
    }

    public int del(Long stuno) {
        SqlSession session = MybatisUtil.get();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            return dao.del(stuno);
        } catch (Exception e) {
            Log.error("删除出错", e);
        } finally {
            MybatisUtil.close(session);
        }
        return 0;
    }

    public Student get(Long stuno) {
        SqlSession session = MybatisUtil.get();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            return dao.get(stuno);
        } finally {
            MybatisUtil.close(session);
        }
    }

    public int update(Student student) {
        SqlSession session = MybatisUtil.get();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            return dao.update(student);
        } finally {
            MybatisUtil.close(session);
        }
    }

    public boolean checkWechat(String we,long stuno) {
        SqlSession session = MybatisUtil.get();
        try {
            StudentDao dao = session.getMapper(StudentDao.class);
            return dao.checkWechat(we,stuno)>0;
        } finally {
            MybatisUtil.close(session);
        }
    }


//
//    public int findTotal() {
//        SqlSession session = MybatisUtil.get();
//        try {
//            SubjectDao dao = session.getMapper(SubjectDao.class);
//            return dao.findTotal();
//        } finally {
//            MybatisUtil.close(session);
//        }
//    }

}
